﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace CustomerUI
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        public static TinyCinema.Bussiness.Login.Customer Customer = new TinyCinema.Bussiness.Login.Customer();
        public Login()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            DataContext = Customer;
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }        

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            (App.Current.MainWindow as MainWindow).blurEffect.Radius = 0;
            this.Close();            
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (txtUsername.Text.Length == 0)
                txtError.Text = App.Current.FindResource("langUserCannotBeEmpty").ToString();
            else if (pwdPassword.Password.Length == 0)
                txtError.Text = App.Current.FindResource("langPasswdCannotBeEmpty").ToString();
            else if ((DataContext as TinyCinema.Bussiness.Login.Customer).Login(txtUsername.Text,pwdPassword.Password))
            {
                (App.Current.MainWindow as MainWindow).blurEffect.Radius = 0;
                this.Close();
                return;
            }
            else txtError.Text = App.Current.FindResource("langIncorrectUserOrPasswd").ToString();
        }

        private void txtUsername_TextChanged(object sender, TextChangedEventArgs e)
        {
            _txtUsername.Visibility = (txtUsername.Text.Length > 0) ? Visibility.Hidden : Visibility.Visible;
        }

        private void pwdPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            _txtPassword.Visibility = (pwdPassword.Password.Length > 0) ? Visibility.Hidden : Visibility.Visible;
        }

        private void InputBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return) btnLogin_Click(null, null);
        }
        
    }
}
